#include "BlockQueue.hpp"
#include "Task.hpp"

using namespace ns_blockqueue;
using namespace ns_task;

void *producter(void *args)
{
    BlockQueue<Task> *bq = (BlockQueue<Task> *)args;
    while (true)
    {
        int x = rand() % 20 + 1;
        int y = rand() % 10 + 1;
        std::string ops = "+-*/%";
        Task task(x, y, ops[rand() % 5]);
        bq->Push(task);
        std::cout << "生产者生产数据为" << task._x << task._op << task._y << std::endl;
        // sleep(1);
        //  sleep(2);
        //  int data = rand()%20 + 1;
        //  bq->Push(data);
        //  std::cout << "生产者生产数据为" << data << std::endl;
    }
}

void *consumer(void *args)
{
    BlockQueue<Task> *bq = (BlockQueue<Task> *)args;
    while (true)
    {
        Task task;
        bq->Pop(&task);
        int res = task.Run();
        std::cout << "消费者处理数据后结果为" << res << std::endl;
        sleep(1);
        // int data;
        // bq->Pop(&data);
        // std::cout << "消费者消费数据为" << data << std::endl;
    }
}
int main()
{
    srand((long long)time(nullptr));
    BlockQueue<Task> *bq = new BlockQueue<Task>(5);

    pthread_t c, p;
    pthread_create(&c, nullptr, consumer, (void *)bq);
    pthread_create(&p, nullptr, producter, (void *)bq);
    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
    return 0;
}